//
//  BloodPressureViewController.m
//  demo
//
//  Created by Robin-Pc on 2017/5/7.
//  Copyright © 2017年 Robin-Pc. All rights reserved.
//

#import "BloodPressureViewController.h"
#import "ARLineChartView.h"
#import "ARLineChartCommon.h"
#import "TimeTools.h"

#define URL_Getbloodsugar @"/api/dailyIndicator/mypressure"

@interface BloodPressureViewController ()
@property(nonatomic, strong)NSArray *dataArr;
@property(nonatomic, strong)ARLineChartView *lineChartView;
@end

@implementation BloodPressureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getbloodsugar] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.dataArr = [BloodPressureViewController convertServerData2LocalArr:responseObject];
        //折线图view
        CGRect rect = CGRectMake (5, 40, self.view.frame.size.width - 5 - 5, 300);
        self.lineChartView = [[ARLineChartView alloc] initWithFrame:rect
                                                         dataSource:self.dataArr
                                                             isDual:YES
                                                             xTitle:@"检查时间"
                                                            y1Title:@"收缩压" y2Title:@"舒张压"
                                                              desc1:@"nonsense" desc2:@"nonsense"
                                                            xLimits:@[]];
        [self.view addSubview:self.lineChartView];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    if (!self.lineChartView) {
        return;
    }
    [self.lineChartView removeFromSuperview];
    self.lineChartView = nil;
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getbloodsugar] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.dataArr = [BloodPressureViewController convertServerData2LocalArr:responseObject];
        //折线图view
        CGRect rect = CGRectMake (5, 40, self.view.frame.size.width - 5 - 5, 300);
        self.lineChartView = [[ARLineChartView alloc] initWithFrame:rect
                                                         dataSource:self.dataArr
                                                             isDual:YES
                                                             xTitle:@"检查时间"
                                                            y1Title:@"收缩压" y2Title:@"舒张压"
                                                              desc1:@"nonsense" desc2:@"nonsense"
                                                            xLimits:@[]];
        [self.view addSubview:self.lineChartView];
    }];
}

//整理服务器返回数据
+ (NSArray*)convertServerData2LocalArr:(NSDictionary*)obj{
    //低压
    NSDictionary *lowPresureArr = [BloodPressureViewController getPresureArr:obj[@"highPressure"]];
    //高压
    NSDictionary *highPresureArr = [BloodPressureViewController getPresureArr:obj[@"lowPressure"]];
    NSArray *lowDateArr = [lowPresureArr allKeys];//低压日期数组
    NSArray *highDateArr = [highPresureArr allKeys];//高压日期数组
    NSMutableArray *allDateArr = [NSMutableArray array];//总日期数组
    for (NSNumber *datekey in lowDateArr) {
        if (![highDateArr containsObject:datekey]) {
            [allDateArr addObject:datekey];
        }
    }
    [allDateArr addObjectsFromArray:highDateArr];
    NSMutableArray *dataArr = [NSMutableArray array];
    for (NSNumber *datekey in allDateArr) {
        RLLineChartItem *point = [[RLLineChartItem alloc]init];
        point.xValue = [datekey doubleValue];
        NSString *val = [lowPresureArr objectForKey:datekey];
        if (val) {
            point.y2Value = [val doubleValue];
        }
        NSString *val2 = [highPresureArr objectForKey:datekey];
        if (val2) {
            point.y1Value = [val2 doubleValue];
        }
        [dataArr addObject:point];
    }
    
    return dataArr;
}

+(NSDictionary*)getPresureArr:(NSArray*)rawArr{
    NSMutableDictionary *presureArr = [NSMutableDictionary dictionary];
    for (NSDictionary *item in rawArr) {
        NSArray *keys = [item allKeys];
        if (keys.count==0) {
            continue;
        }
        NSString *datekey = keys[0];
        NSString *val = item[datekey];
        NSDate *date = [TimeTools getDateWithString:datekey];
        long timeVal = [date timeIntervalSince1970];
        if ([val isNotEmpty]) {
            [presureArr setObject:val  forKey:[NSNumber numberWithLong:timeVal]];
        }
    }
    return presureArr;
}

@end
